package com.schoolCommunity.util;

import com.alibaba.fastjson.JSONArray;
import com.schoolCommunity.entity.ConfessionWall;
import com.schoolCommunity.entity.Member;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class SearchES {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    //按照昵称，账号等搜索用户
    //传入昵称，邮箱，起始页数
    public List<ConfessionWall> searchConfessionWall(String title, String text, int pageNo) throws IOException {

        if (pageNo == 0)
            pageNo = 1;
        if (title.equals(""))
            title = null;
        if (text.equals(""))
            text = null;

        List<ConfessionWall> list = new ArrayList<>();

        //条件搜索
        //搜索的索引
        SearchRequest searchRequest = new SearchRequest("confession_wall");
        //创建搜索条件构造器
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

//        先判断传来的数据
        //完全符合条件
        if (text != null && title != null) {
            System.out.println("主题内容关键字均正常");
            //构建查询条件
            searchSourceBuilder.query(QueryBuilders.boolQuery()
                    //匹配查询条件
                    .must(QueryBuilders.matchQuery("title", title))
                    //可以设置多条匹配
                    .must(QueryBuilders.matchQuery("text", text))

            )
                    //设置分页查询，从0开始，查20条
                    .from(pageNo).size(20);
        } else if (text != null && title == null) {
            System.out.println("没有主题，使用内容关键字比较");
            //构建查询条件
            searchSourceBuilder.query(QueryBuilders.boolQuery()
                            //匹配查询条件
//                    .must(QueryBuilders.matchQuery("nickname",nickname))
                            //可以设置多条匹配
                            .must(QueryBuilders.matchQuery("text", text))

            )
                    //设置分页查询，从0开始，查20条
                    .from(pageNo).size(20);
        } else if (text == null && title != null) {
            System.out.println("没有内容，使用主题比较");
            //构建查询条件
            searchSourceBuilder.query(QueryBuilders.boolQuery()
                            //匹配查询条件
                            .must(QueryBuilders.matchQuery("title", title))
                    //可以设置多条匹配
//                    .must(QueryBuilders.matchQuery("email",email))

            )
                    //设置分页查询，从0开始，查20条
                    .from(pageNo).size(20);
        } else {
            System.out.println("主题内容都没有");
            return list;
        }


        //执行搜索
        searchRequest.source(searchSourceBuilder);
        //获取搜索结果
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //解析结果
//        List<Map<String,Object>> list = new ArrayList<>();

//        返回结果

        for (SearchHit documentFields : searchResponse.getHits().getHits()) {

            Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();
            Object jsonO = JSONArray.toJSON(sourceAsMap);
            String json = jsonO.toString();
            ConfessionWall confessionWall = JSONArray.parseObject(json, ConfessionWall.class);
            list.add(confessionWall);

        }
        //返回member对象list
        return list;

    }


}
